//
//  HAODE
//
//  Created by panquanReabam.com on 15/9/23.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "OrderDetailViewController.h"
#import "MJRefresh.h"
#import "MemberListViewController.h"
#import "ChoosePaymentViewController.h"
#import "MembershipDetailsViewController.h"
#import "OrderDetailView.h"
#import "CommonDetailView.h"
#import "OrderDetailModel.h"
#import "DeliveryConfirmTheOrderViewController.h"
#import "PrinterListViewController.h"
#import "SVProgressHUD.h"
#import <PgyUpdate/PgyUpdateManager.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "GetAddBuyGoodsCell.h"
#import "OrderDetailM.h"
#import "AllotOrderDisposeViewController.h"
@interface OrderDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate,DeliveryConfirmTheOrderViewControllerDelegate,CommonDetailViewDelegate,AllotOrderDisposeViewControllerDelegate>
@end
@implementation OrderDetailViewController
{
    UIButton* operationsBtn;
    CommonDetailView*CommonView;
    OrderDetailView*DetailView;
    UIScrollView*mainScroll;
    NSDictionary*dataDict;
    UIView*operationsView;
    NSArray*ColorArray;
    NSArray* operationsArray;
    NSMutableArray*deviceList;
    BOOL saveAccount;
    UITableView*table;
    NSMutableArray*mItemsArray;
    NSMutableArray*ModelArray;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
   
    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
    if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) {
        NSUserDefaults * pushJudge = [NSUserDefaults standardUserDefaults];
        [pushJudge setObject:@""forKey:@"push"];
        [pushJudge synchronize];//记得立即同步
        [self dismissViewControllerAnimated:YES completion:nil];

    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }

   
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}

-(void)viewWillAppear:(BOOL)animated{

//    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [super viewWillAppear:YES];
    if ([self.sat isEqualToString:@"cashier"]) {
        
    }else{
    [PrinterWraper SetBlutoothDelegate:self];
    [PrinterWraper StartScanTimeout:10];
    }
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [PrinterWraper StopScan];
    [PrinterWraper SetBlutoothDelegate:nil];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    deviceList=[[NSMutableArray alloc]init];
    deviceList=nil;
    [self update:@""];
}

-(void)updateBluetoothDevice:(NSMutableArray*)devices;
{
    deviceList=[[NSMutableArray alloc]init];
    deviceList  =devices;
    NSLog(@"-----%@",devices);
}
-(void)BlueToothOpen:(BOOL)isopen;{
    deviceList=nil;
}
-(void)Printer{
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Device = [user objectForKey:@"Device"];
//  NSLog(@"------%@",Device);
    if (deviceList.count>0) {
       
        if (Device.length>0) {
             [Global showIndeterminateHudSub:@"打印连接中，请稍后。。。" delay:30 view:self.view];
            //*****************************************************//

            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:1 rotation:0];// 3 大字体  ，7默认行间距,1局中对齐
            [PrinterWraper addPrintText:@"欢迎光临\n\n"];//打印文字
       
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"收银员: %@  \n", [[dataDict objectForKey:@"order"] objectForKey:@"source"] ]];
            
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"收款时间: %@  \n", [[dataDict objectForKey:@"order"] objectForKey:@"orderDate"] ]];
            
            NSDictionary *value = [dataDict objectForKey:@"member"];
//           NSLog(@"---%ld",value.count);
            if (value.count==0)//零售会员
            {
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
                [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员名称: %@  \n", @"零售会员" ]];
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
                [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员卡号: %@  \n", @"--------"]];
            }else{
                
                
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
                [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员名称: %@  \n", [[dataDict objectForKey:@"member"] objectForKey:@"memberName"] ]];
               
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
                
                
                
                if ( [[Global getJudgeTheErrorStr:[[dataDict objectForKey:@"member"] objectForKey:@"cardNo"]]isEqualToString:@""]||[[dataDict objectForKey:@"member"] objectForKey:@"cardNo"] ==NULL) {
                    [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员卡号: %@  \n", @""]];
               
                }else{
                    [PrinterWraper addPrintText:[NSString stringWithFormat: @"会员卡号: %@  \n", [[dataDict objectForKey:@"member"] objectForKey:@"cardNo"] ]];
                }
                }
         
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"门店名称: %@  \n", [[dataDict objectForKey:@"order"] objectForKey:@"companyName"] ]];
         
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"客服电话: %@  \n", [[dataDict objectForKey:@"companyBaseInfo"] objectForKey:@"phone"] ]];
            
       
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:@"--------------------------------\n\n"];
           
            
            
            for (NSDictionary* dicc in  [dataDict objectForKey:@"orderItem"] )
            {
                if ([[NSString stringWithFormat:@"%@",[dataDict valueForKey:@"receiptTemplate"]] isEqualToString:@"TemplateB"]) {
                   
                    [PrinterWraper setPrintFormat:1 LineSpace:1 alinment:0 rotation:0];
                    [PrinterWraper addPrintBarcode:[NSString stringWithFormat:@"%@",[dicc valueForKey:@"skuBarcode"]] isTwoDimensionalCode:0];//1维码
//                    NSLog(@"------%@",[NSString stringWithFormat:@"%@",[dicc valueForKey:@"skuBarcode"]]);
                }else{

                }
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
                [PrinterWraper addPrintText:[NSString stringWithFormat:@"%@\n",[dicc valueForKey:@"itemName"]]];
                
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:2 rotation:0];
                [PrinterWraper addPrintText:[NSString stringWithFormat:@"%@x%@   小计：%@\n",[dicc valueForKey:@"dealPrice"],[dicc valueForKey:@"quantity"],[dicc valueForKey:@"totalMoney"]]];
                [PrinterWraper addPrintText:@"\n"];//打印文字
                
            }
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            
            [PrinterWraper addPrintText:@"--------------------------------\n\n"];
     
            

//            //  NSArray *headers =@[@"商品名称",@"单价",@"小计"];
//            NSMutableArray*MutableArray1=[[NSMutableArray alloc]init];
//            
//            for (NSDictionary* dicc in  [dataDict objectForKey:@"orderItem"] )
//            {
//                [MutableArray1 addObject:[NSString stringWithFormat:@"%@\n",[dicc valueForKey:@"itemName"]]];
//                [MutableArray1 addObject:[NSString stringWithFormat:@"%@x%@",[dicc valueForKey:@"dealPrice"],[dicc valueForKey:@"quantity"]]];
//                [MutableArray1 addObject:[NSString stringWithFormat:@"   小计：%@\n",[dicc valueForKey:@"totalMoney"]]];
//                
//            }
//   
//            NSArray* body =@[MutableArray1];
//            //打印商品列表，会自动排版，要求数组长度一致，空白地方用@""
//            
//            [PrinterWraper addItemLines:body];
//    
//            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
//    
//            [PrinterWraper addPrintText:@"--------------------------------\n\n"];
//            
//            
            
            
            NSNumber*Total = [[dataDict objectForKey:@"order"] objectForKey:@"totalMoney"];
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            [numberFormatter setPositiveFormat:@"###,##0.00;"];
            NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:Total.doubleValue]];
            
            NSNumber*orderPreferentia = [[dataDict objectForKey:@"order"] objectForKey:@"preferentialMoney"];
            NSString *formattedNumberString1 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:orderPreferentia.doubleValue]];
            
            NSNumber*discount = [[dataDict objectForKey:@"order"] objectForKey:@"discountMoney"];
            NSString *formattedNumberString2 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:discount.doubleValue]];
            
            NSNumber*freight = [[dataDict objectForKey:@"order"] objectForKey:@"expressFee"];
            NSString *formattedNumberString3 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:freight.doubleValue]];
            
            NSNumber*preferential = [[dataDict objectForKey:@"order"] objectForKey:@"couponMoney"];
            NSString *formattedNumberString4 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:preferential.doubleValue]];
            
            NSNumber*realMoney = [[dataDict objectForKey:@"order"] objectForKey:@"realMoney"];
            NSString *formattedNumberString5 = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:realMoney.doubleValue]];
            
            
            
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"商品总额: %@  \n",formattedNumberString ]];
            
            
            if ([formattedNumberString1 isEqualToString:@"0.00"]) {
                
            }else{
                [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
                [PrinterWraper addPrintText:[NSString stringWithFormat: @"活动优惠: %@  \n",formattedNumberString1 ]];
            }
            
            if ([formattedNumberString2 isEqualToString:@"0.00"]) {
                
            }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"抹零折扣: %@  \n",formattedNumberString2 ]];
            }
            if ([formattedNumberString3 isEqualToString:@"0.00"]) {
                
            }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"运费: %@  \n",formattedNumberString3 ]];
            }
            
            if ([formattedNumberString4 isEqualToString:@"0.00"]) {
                
            }else{
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"优惠券: %@  \n",formattedNumberString4 ]];
            }
            
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"实付款: %@  \n",formattedNumberString5 ]];
            
            
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:[NSString stringWithFormat: @"支付方式: %@  \n",[[dataDict objectForKey:@"order"] objectForKey:@"payTypeName"] ]];
         
            [PrinterWraper setPrintFormat:1 LineSpace:58 alinment:0 rotation:0];
            [PrinterWraper addPrintText:@"--------------------------------\n\n"];
          
            [PrinterWraper setPrintFormat:1 LineSpace:7 alinment:0 rotation:0];
            [PrinterWraper addPrintText:@"谢谢光临，欢迎下次惠顾\n"];
            
            [PrinterWraper addPrintText:@"\n\n"];
           
            [PrinterWraper connectPrinter:Device shouldreset:YES];
            //*****************************************************//
        }else{
            
            PrinterListViewController *detail=[[PrinterListViewController alloc] init];
            detail.Dic=dataDict;
            [self.navigationController pushViewController:detail animated:YES];
            }
    
    }else{
        [self showAlertMessage:@"蓝牙未开!"];
    }
}
-(void)didConnected:(NSString*)deviceUid  Result:(BOOL)success;{
       if (success)
        [self excuteTask];
    
}
-(void)excuteTask{
    [PrinterWraper startPrint:nil];
    [Global hideProgressHUDInternal:self.view];
    [Global showTextHudSub:@"打印成功！" view:self.view];

}

-(void)memberBtn:(NSString *)str{
    MembershipDetailsViewController *detailViewController = [[MembershipDetailsViewController alloc] init];
    detailViewController.memberId=[NSString stringWithFormat:@"%@",[[dataDict objectForKey:@"member"]objectForKey:@"memberId"]];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}
- (void)update:(NSString *)str{
    
    [self setNavi];
    [mainScroll removeFromSuperview];
    [operationsBtn setTitle:@"" forState:UIControlStateNormal];
    [operationsBtn removeFromSuperview];
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,64, ScreenWidth, ScreenHeight-64)];
    [mainScroll setBackgroundColor: zuiqianhuiColor];
    mainScroll.delegate = self;
    mainScroll.userInteractionEnabled = YES;
    mainScroll.scrollEnabled = YES;
    mainScroll.showsVerticalScrollIndicator = FALSE;
    mainScroll.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:mainScroll];
    
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [table registerClass:[GetAddBuyGoodsCell class] forCellReuseIdentifier:@"OrderDetailViewController"];
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];

    
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.orderId forKey:@"orderId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Order/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"-----------%@", severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                dataDict=severDataDict.dataDict;
                CommonView = [[CommonDetailView alloc]init];
                CommonView.mDelegate=self;
                OrderDetailModel*dataModel=[OrderDetailModel dataModelWithOrderDetailDict:severDataDict.dataDict];
                CommonView.model=dataModel;
                CommonView.frame=CGRectMake(0, 0, ScreenWidth, CommonView.heightView.bottom);

                table.tableHeaderView=CommonView;
                
                mItemsArray =[[NSMutableArray alloc]init];
                ModelArray=[[NSMutableArray alloc]init];
                mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"orderItem"]];
               
                if (mItemsArray.count>0) {
                    for (NSDictionary *dic in mItemsArray) {
                        OrderDetailM *dataModel=[OrderDetailM mj_objectWithKeyValues:dic];
                        [ModelArray addObject:dataModel];
                        
                    }

                }
                [table reloadData];

                DetailView = [[OrderDetailView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,400)];
                OrderDetailModel*dataModel1=[OrderDetailModel dataModelWithOrderDetailDict:severDataDict.dataDict];
                DetailView.model=dataModel1;
                
                table.tableFooterView=DetailView;
                table.backgroundColor=zuiqianhuiColor;
                operationsArray=nil;
                operationsArray=[[NSArray alloc]init];
                
                if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"NA"]) {
                    
                    [self operations];
                    
                    
                }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"NT"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"NP"]){
                    
                    [self operations];
                    
                    
                }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"NT"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
                    
                    
                    [self operations];
                    
                    
                }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"ND"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
                    
                    [self operations];
                    
                    
                }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"ND"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"NP"]){
                    
                    [self operations];
                    
                    
                }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"BT"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
                    [self operations];

                }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"BD"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
                    [self operations];
                }else{
                
                 [self operations];
                }
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:requestResultStr delegate:self cancelButtonTitle:@"确定返回" otherButtonTitles: nil];
                alert.tag=0;
                [alert show];
                
            }
        }else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
}




#pragma mark - Table view data source
#pragma mark - Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ModelArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GetAddBuyGoodsCell*cell = [tableView dequeueReusableCellWithIdentifier:@"OrderDetailViewController"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell==nil) {
        
    }else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
        }
    }

    OrderDetailM *dataModel=ModelArray[indexPath.row];
    
  
                if ([[NSString stringWithFormat:@"%@",dataModel.productType]isEqualToString:@"Exchange"]||[[NSString stringWithFormat:@"%@",dataModel.productType]isEqualToString:@"Gift"]) {
    
                    UIView*goodsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 90)];
     
                    UIImageView*headImageFull = [[UIImageView alloc]init];
                    [headImageFull.layer setCornerRadius:4.0];
                    [headImageFull.layer setBorderWidth:0.5];
                    [headImageFull.layer setMasksToBounds:YES];
                     [headImageFull setImageWithURL:[NSURL URLWithString:  dataModel.headImageFull] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
                    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
                    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0.93, 0.93, 0.93, 0.93 });
                    [headImageFull.layer setBorderColor:colorref2];//边框颜色
                    headImageFull.frame = CGRectMake(15, 15, 60, 60);
                    [goodsView addSubview:headImageFull];
    
    
                    NSString *s1 =[NSString stringWithFormat:@"%@",dataModel.productTypeName];
                    UIFont *font1 = [UIFont systemFontOfSize:11];
                    CGSize size1 = CGSizeMake(ScreenWidth-60, 2000);
                    CGSize labelsize1 = [s1 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
    
                    UILabel*conditions=[[UILabel alloc] initWithFrame:CGRectMake(headImageFull.right+10, 15, labelsize1.width, 12)];
                    conditions.backgroundColor=[UIColor redColor];
                    [conditions setText:[NSString stringWithFormat:@"%@",s1]];
                    conditions.font=[UIFont systemFontOfSize:10];
                    conditions.textAlignment=NSTextAlignmentCenter;
                    conditions.textColor=[UIColor whiteColor];
                    [goodsView addSubview:conditions];
    
    
                    NSString *s2 =[NSString stringWithFormat:@"%@", dataModel.itemName];
                    UIFont *font2 = [UIFont systemFontOfSize:13.5];
                    CGSize size2 = CGSizeMake(ScreenWidth-60-labelsize1.width-15-30, 2000);
                    CGSize labelsize2 = [s2 sizeWithFont:font2 constrainedToSize:size2 lineBreakMode:NSLineBreakByCharWrapping];
    
                    UILabel*itemName = [[UILabel alloc]init];
                    itemName.text=s2;
                    itemName.frame = CGRectMake( conditions.right+5, 13.5, ScreenWidth-60-labelsize1.width-15-30, labelsize2.height);
                    itemName.numberOfLines=0;
                    itemName.textColor=qianheiColor;
                    itemName.font=[UIFont systemFontOfSize:13.5];
                    [goodsView addSubview:itemName];
    
    
                    UILabel*itemType = [[UILabel alloc]init];
                    itemType.frame = CGRectMake(conditions.right+5, 45, ScreenWidth-120, 13);
                    itemType.font=[UIFont systemFontOfSize:12];
                    itemType.textColor=shenhuiColor;
                    itemType.text=[NSString stringWithFormat:@"%@",dataModel.specName];
                    [goodsView addSubview:itemType];
    
                    NSString *s =[NSString stringWithFormat:@"数量 %@",dataModel.quantity];
                    UIFont *font = [UIFont systemFontOfSize:12];
                    CGSize size = CGSizeMake(100, 2000);
                    CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
                    UILabel*quantity = [[UILabel alloc]init];
                    quantity.frame = CGRectMake(conditions.right+5, 60, labelsize.width,12);
                    quantity.font=[UIFont systemFontOfSize:12];
                    quantity.textColor=shenhuiColor;
                    quantity.text=[NSString stringWithFormat:@"数量 %@",dataModel.quantity];
                    [goodsView addSubview:quantity];
    
    
                    UILabel*dealPrice = [[UILabel alloc]init];
                    dealPrice.frame = CGRectMake(quantity.right+20, 60, 230,13);
                    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                    [numberFormatter setPositiveFormat:@"###,##0.00;"];
                     NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:dataModel.dealPrice.doubleValue]];
                                     dealPrice.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString];
                    dealPrice.textColor=qianheiColor;
                    dealPrice.font=[UIFont systemFontOfSize:13];
                    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
    
                    [AttributedStr addAttribute:NSFontAttributeName
    
                                          value:[UIFont systemFontOfSize:12]
    
                                          range:NSMakeRange(0, 1)];
    
                    [AttributedStr addAttribute:NSForegroundColorAttributeName
    
                                          value:shenhuiColor
    
                                          range:NSMakeRange(0, 1)];
                    dealPrice.attributedText = AttributedStr;
    
    
                    [goodsView addSubview:dealPrice];
                    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 90-0.40, ScreenWidth-20, 0.40)];
                    lineLabel1.backgroundColor=qianhuiColor;
                    [goodsView addSubview:lineLabel1];
                    [cell.contentView addSubview:goodsView];
    
                }else{
  
    UIView*goodsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 120)];
    
    UIImageView*headImageFull = [[UIImageView alloc]init];
    [headImageFull.layer setCornerRadius:4.0];
    [headImageFull.layer setBorderWidth:0.5];
    [headImageFull.layer setMasksToBounds:YES];
    [headImageFull setImageWithURL:[NSURL URLWithString:  dataModel.headImageFull] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0.93, 0.93, 0.93, 0.93 });
    [headImageFull.layer setBorderColor:colorref2];//边框颜色
    headImageFull.frame = CGRectMake(15, 17.5, 82, 82);
    [goodsView addSubview:headImageFull];
    
    
    NSString *s2 =[NSString stringWithFormat:@"%@", dataModel.itemName];
    UIFont *font1 = [UIFont systemFontOfSize:13.5];
    CGSize size1 = CGSizeMake(ScreenWidth-82-15-30, 2000);
    CGSize labelsize1 = [s2 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
    UILabel*itemName = [[UILabel alloc]init];
    itemName.numberOfLines=0;
    itemName.text=s2;
    itemName.frame = CGRectMake( headImageFull.right+10, 17.5, ScreenWidth-82-15-30, labelsize1.height);
    itemName.textColor=qianheiColor;
    itemName.font=[UIFont systemFontOfSize:13.5];
    [goodsView addSubview:itemName];
    
    
    UILabel*itemType = [[UILabel alloc]init];
    itemType.frame = CGRectMake( headImageFull.right+10, 68, ScreenWidth-120, 13);
    itemType.font=[UIFont systemFontOfSize:12];
    itemType.textColor=shenhuiColor;
    itemType.text=[NSString stringWithFormat:@"%@",dataModel.specName];
    [goodsView addSubview:itemType];
    
    NSString *s =[NSString stringWithFormat:@"数量 %@",dataModel.quantity];
    UIFont *font = [UIFont systemFontOfSize:12];
    CGSize size = CGSizeMake(100, 2000);
    CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];

    UILabel*quantity = [[UILabel alloc]init];
    quantity.frame = CGRectMake(headImageFull.right+10, 85, labelsize.width,12);
    quantity.font=[UIFont systemFontOfSize:12];
    quantity.textColor=shenhuiColor;
    quantity.text=[NSString stringWithFormat:@"数量 %@",dataModel.quantity];
    [goodsView addSubview:quantity];
    
    
    UILabel*dealPrice = [[UILabel alloc]init];
    dealPrice.frame = CGRectMake(quantity.right+20, 85, 230,13);
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
                    NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:dataModel.dealPrice.doubleValue]];
    dealPrice.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString];
                  
    dealPrice.textColor=qianheiColor;
    dealPrice.font=[UIFont systemFontOfSize:13];
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@" ,formattedNumberString]];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:12]
     
                          range:NSMakeRange(0, 1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:shenhuiColor
     
                          range:NSMakeRange(0, 1)];
    dealPrice.attributedText = AttributedStr;
    
    [goodsView addSubview:dealPrice];
      
                    
    if ([[NSString stringWithFormat:@"%@",  [[dataDict objectForKey:@"order"] objectForKey:@"docType"]] isEqualToString:@"Integral"]){
                    
    UILabel*integral = [[UILabel alloc]init];
    integral.frame = CGRectMake(ScreenWidth-160, 85, 145,13);
    integral.font=[UIFont systemFontOfSize:13];
    integral.textColor=hongseColor;
    integral.textAlignment=NSTextAlignmentRight;
    integral.text=[NSString stringWithFormat:@"兑换积分 %@",dataModel.integral];
    NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"兑换积分 %@",dataModel.integral]];
        
    [AttributedStr1 addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:13]
         
                              range:NSMakeRange(0, 4)];
        
    [AttributedStr1 addAttribute:NSForegroundColorAttributeName
         
                              value:zhonghuiColor
         
                              range:NSMakeRange(0, 4)];
    integral.attributedText = AttributedStr1;

    [goodsView addSubview:integral];
    }
    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 120-0.40, ScreenWidth-20, 0.40)];
    lineLabel1.backgroundColor=qianhuiColor;
    [goodsView addSubview:lineLabel1];
    
    [cell.contentView addSubview:goodsView];
    }

    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
     OrderDetailM *dataModel=ModelArray[indexPath.row];
    
    if ([[NSString stringWithFormat:@"%@",dataModel.productType]isEqualToString:@"Exchange"]||[[NSString stringWithFormat:@"%@",dataModel.productType]isEqualToString:@"Gift"]) {
        return  90;

    }
    
    
    return  120;
}
-(void)operations:(UIButton*)sender{
    
   
    saveAccount=!saveAccount;
    if (saveAccount) {
    
    if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"NA"]) {
        
        operationsArray=@[@"确认受理",@"拒绝受理",@"打印小票"];
        [self Btn];
    }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"NT"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"NP"]){
        
        operationsArray=@[@"取消订单",@"补收银",@"打印小票"];
        [self Btn];
        
    }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"NT"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
        
        operationsArray=@[@"确认提货",@"打印小票"];
        [self Btn];
        
    }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"ND"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
        
        operationsArray=@[@"确认配送",@"打印小票"];
        [self Btn];
        
    }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"ND"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"NP"]){
           operationsArray=@[@"取消订单",@"补收银",@"打印小票"];
        [self Btn];
    }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"BT"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
        operationsArray=@[@"部分提货",@"打印小票"];
        [self Btn];
    }else if ([[[dataDict objectForKey:@"order"]objectForKey:@"orderStatus"]isEqualToString:@"BD"]&&[[[dataDict objectForKey:@"order"]objectForKey:@"payStatus"]isEqualToString:@"YP"]){
        operationsArray=@[@"部分配送",@"打印小票"];
        [self Btn];
    }else{
        operationsArray=@[@"打印小票"];
        [self Btn];
    }
    
    }else{
    
     [operationsView removeFromSuperview];
    }
}

-(void)Btn{
    
    operationsView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 110)];
    operationsView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:operationsView];
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    ColorArray =[NSArray arrayWithObjects:[UIColor colorWithRed:78/255.0 green:112/255.0 blue:22/255.0 alpha:1.0],[UIColor colorWithRed:90/255.0 green:130/255.0 blue:26/255.0 alpha:1.0],[UIColor colorWithRed:107/255.0 green:149/255.0 blue:46/255.0 alpha:1.0],nil];
    
    
    for (int i = 0; i<operationsArray.count; i++) {
        
        UIButton *optName = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-168/2*(i+1),0, 168/2, 110)];
        [optName setTitle:operationsArray[i]  forState:UIControlStateNormal];
        optName.tag=i;
        [optName setTitleColor:[UIColor whiteColor] forState:0];
        optName.titleLabel.font=[UIFont systemFontOfSize:13];
        optName.backgroundColor=ColorArray[i];
        [optName addTarget:self action:@selector(btnTouchUpInsid:) forControlEvents:UIControlEventTouchUpInside];
        [operationsView addSubview:optName];
    }
    
    
}
-(void)btnTouchUpInsid:(UIButton*)sender{
    [operationsView removeFromSuperview];
    if ([operationsArray[sender.tag] isEqualToString:@"打印小票"]) {
    
        [self Printer];
    
    }else
    if ([operationsArray[sender.tag] isEqualToString:@"补收银"]) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:[[dataDict objectForKey:@"order"] objectForKey:@"orderId"] forKey:@"orderId"];
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        [HttpRequest requestData:@"Business/Order/ToResiduePay" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"-----------%@", severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    ChoosePaymentViewController  *generateVC=[[ChoosePaymentViewController alloc] init];
                    generateVC.Dict=severDataDict.dataDict;
                    generateVC.mcardPay=[severDataDict.dataDict objectForKey:@"mcardPay"];
                    [self.navigationController pushViewController:generateVC animated:YES];
                }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                    }}
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
            
        }];
    }else
    if ([operationsArray[sender.tag] isEqualToString:@"确认配送"]||[operationsArray[sender.tag] isEqualToString:@"部分配送"]) {
        DeliveryConfirmTheOrderViewController  *generateVC=[[DeliveryConfirmTheOrderViewController alloc] init];
        generateVC.orderId=[[dataDict objectForKey:@"order"] objectForKey:@"orderId"];
        generateVC.mDelegate=self;
        generateVC.sat=@"订单详情确认配送";
        [self.navigationController pushViewController:generateVC animated:YES];
    }else
        if ([operationsArray[sender.tag] isEqualToString:@"取消订单"]) {
            AllotOrderDisposeViewController *detailViewController = [[AllotOrderDisposeViewController alloc] init];
            detailViewController.Type=@"Order";
            detailViewController.mDelegate=self;
            detailViewController.titleName=@"取消订单";
            detailViewController.orderId=self.orderId;
            [self.navigationController pushViewController:detailViewController animated:YES];
        }
        else
            if ([operationsArray[sender.tag] isEqualToString:@"确认受理"]) {

                AllotOrderDisposeViewController *detailViewController = [[AllotOrderDisposeViewController alloc] init];
                detailViewController.Type=@"Order";

                detailViewController.optType=@"1";
                detailViewController.mDelegate=self;
                detailViewController.titleName=@"确认受理";
                detailViewController.orderId=self.orderId;
                [self.navigationController pushViewController:detailViewController animated:YES];
            }else
                if ([operationsArray[sender.tag] isEqualToString:@"拒绝受理"]) {
                    AllotOrderDisposeViewController *detailViewController = [[AllotOrderDisposeViewController alloc] init];
                    
                    detailViewController.Type=@"Order";
                    detailViewController.optType=@"2";
                    detailViewController.mDelegate=self;
                    detailViewController.titleName=@"拒绝受理";
                    detailViewController.orderId=self.orderId;
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }else
                    if ([operationsArray[sender.tag] isEqualToString:@"确认提货"]||[operationsArray[sender.tag] isEqualToString:@"部分提货"]) {
                        DeliveryConfirmTheOrderViewController  *generateVC=[[DeliveryConfirmTheOrderViewController alloc] init];
                        generateVC.orderId=[[dataDict objectForKey:@"order"] objectForKey:@"orderId"];
                        generateVC.mDelegate=self;
                        generateVC.sat=@"订单详情确认提货";
                        [self.navigationController pushViewController:generateVC animated:YES];
                        
                        
                    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag==0) {
        if (buttonIndex == 0) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
        }
        
    }
    
}

-(void)closeKeyBoad
{
    [operationsView removeFromSuperview];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"订单详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
}
-(void)operations{
    
    operationsBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [operationsBtn setTitle:@"···" forState:UIControlStateNormal];
    operationsBtn.titleLabel.font=[UIFont systemFontOfSize:28];
    [operationsBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
    [operationsBtn addTarget:self action:@selector(operations:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:operationsBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    
}

@end
